

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * A very simple logging class that prints to either standard error or
 * standard output, while adding timestamps to the messages.
 * 
 * @author Daniel M. Zimmerman
 * @author James Marquradt
 * @version Fall 2013
 */
public final class Log
{
	/**
	 * The date formatter to be used.
	 */
	private static final SimpleDateFormat DATE = 
			new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSS", Locale.US);

	/**
	 * The separator between the date and the message.
	 */
	private static final String SEPARATOR = ": ";

	/**
	 * A constructor to prevent instantiation.
	 */
	private Log()
	{
		// do nothing
	}

	/**
	 * Print a log message to standard output.
	 * 
	 * @param the_message The log message.
	 */
	public static synchronized void out(final String the_message)
	{
		System.out.println(DATE.format(new Date()) + SEPARATOR + the_message);
	}

	/**
	 * Print a log message to standard error.
	 * 
	 * @param the_message The log message.
	 */
	public static synchronized void err(final String the_message)
	{
		System.err.println(DATE.format(new Date()) + SEPARATOR + the_message);
	}

	/**
	 * Print a log message to the node's log file.
	 * 
	 * @param the_message The log message.
	 * @param the_id The id of the node.
	 * @throws FileNotFoundException
	 */
	public static synchronized void toFile(final String the_message, final int the_id) 
			throws FileNotFoundException
			{
		PrintStream write = new PrintStream(
			     new FileOutputStream("log_node_"+the_id+".txt", true));
		
		write.println(the_message);
		write.close();
	}


	public static void toFile(final String the_message) throws FileNotFoundException
	{

		PrintStream write = new PrintStream(
			     new FileOutputStream("log_node_registry.txt", true));
		
		write.println(the_message);
		write.close();

	}
}
